/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wat.ziszcommon.remote;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import wat.ziszcommon.clientgui.FunctionBase;
import wat.ziszcommon.invoker.MethodInvoker;

/**
 *
 * @author Krzysztof Wilkos
 */
public class RemoteInvocationHandler implements InvocationHandler {

    private MethodInvoker invoker;

    public RemoteInvocationHandler(MethodInvoker invoker) {
        this.invoker = invoker;
    }

    public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
        Class[] interfaces = obj.getClass().getInterfaces();
        for (Class interFace : interfaces) {
            Class[] ints = interFace.getInterfaces();
            if (ints != null) {
                for (Class i : ints) {
                    if (i.equals(FunctionBase.BaseServStub.class)) {
                        return invoker.invokeMethod(interFace.getName(), method.getName(), args);
                    }
                    if (i.equals(ServerInterface.class)) {
                        return invoker.invokeMethod(interFace.getName(), method.getName(), args);
                    }
                }
            }
        }
        return null;
    }
}
